package com.appspot.bibtable.model.format;

import java.util.ArrayList;
import java.util.List;

public class ListParser extends InputParser
{
	public ListParser()
	{
		super();
	}

	public ListParser(InputParser elementParser)
	{
		super(elementParser);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object parse(String value)
	{
		if (value == null)
		{
			return null;
		}
		if (value.startsWith("[") && value.endsWith("]"))
		{
			value = value.substring(1, value.length() - 1);
		}
		if (value.length() == 0)
		{
			return null;
		}

		InputParser elementParser = getElementParser();
		if (elementParser == null)
		{
			elementParser = new DefaultInputParser();
		}

		List list = new ArrayList();
		for (String element : value.split("\\s*;\\s*"))
		{
			Object parsedElement = elementParser.parse(element);
			if (parsedElement != null)
			{
				list.add(parsedElement);
			}
		}

		return list;
	}
}
